const express=require("express");
const app=express();
app.use(express.json());
const mysql = require('mysql');

// 数据库连接配置
const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: 'wangbin',
  database: 'zhsx',
  connectionLimit: 10, //调整连接池大小
  waitForConnections: true,
  queueLimit: 0 // 队列最大长度，0 表示无限
});

  module.exports = pool;
//设置跨域
app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  next();
});
app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});
//引用路由文件
let chartOne=require("./router/one");
let chartTwo=require("./router/two");
let chartThree=require("./router/three");
let chartFour=require("./router/four");

// 添加新的 CRUD 路由
let deviceRouter = require("./router/deviceRouter");
app.use("/api", deviceRouter);

let loginRouter = require("./router/denglu"); // 确保路径正确
app.use("/api-login", loginRouter); // 将登录路由挂载到/api路径下
//使用中间件来配置路由
app.use("/one",chartOne)
app.use("/two",chartTwo)
app.use("/three",chartThree)
app.use("/four",chartFour)

app.listen(8888, () => {
  console.log('Server is running on port 8888');
});
